layui.config({
    base: '../../layui/' //此处路径请自行处理, 可以使用绝对路径
}).use(['jquery', 'form','layer'], function () {
    const form = layui.form
        , layer = layui.layer
        , $ = layui.jquery;
    $("#close").on('click', function () {
        const index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    });
    form.on('submit(save)', function (data) {
        const params = data.field;
        submit($, params);
        return false;
    });
    $("#checkCron").on('click', function () {
        $.ajax({
            url: '/monitor/job/timeExpression?timeExpression=' + $("#cronExpression").val(),
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            type: "get",
            success: function (result) {
                let html = ""
                let list = $("#cronList");
                if (result.code = 200) {
                    let data = result.data
                    for (let i = 0; i < data.length; i++) {
                        html += "<div>" + data[i] + "</div>"
                    }
                    list.append(html)
                }else {
                    html = result.desc
                }
                list.append(html)
                layer.open({
                    type: 2,
                    title: '校验表达式',
                    area: ['700px', '300px'],
                    fix: false, //
                    closeBtn: false,
                    shift: 2,
                    shadeClose: true,
                    content: ["checkCron.html", 'no'],
                    success: function (layero, index) {
                        const body = layer.getChildFrame('body', index);//获取弹出层的dom元素
                        body.find("#cronExpression").val($("#cronExpression").val());
                        body.find("#cronList").append(html);
                    },
                    yes: function () {
                    }
                });
            }
        })

    });
    function submit($, params) {
        $.ajax({
            url: '/monitor/job',
            data: JSON.stringify(params),
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            type: "PUT",
            success: function (result) {
                layer.msg(result.desc, {icon: 1, time: 1000}, function () {
                    CloseWin();
                })
            }
        })
    }
    //关闭页面
    function CloseWin() {
        parent.location.reload(); // 父页面刷新
        const index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }

});

